package com.nulldev.util.os;

import java.io.File;

import com.nulldev.util.io.FileUtil;
import com.nulldev.util.java.JVMUtil;
import com.nulldev.util.java.ProcessUtil;
import com.nulldev.util.other.ArrayUtils;
import com.nulldev.util.string.StringsUtil;

public class BIOSUtil {

	public static class BIOS {
		private final String bios_date;
		private final String bios_vendor;
		private final String bios_version;
		
		private final String board_name;
		
		public BIOS(final String bd, final String bv, final String bve, final String board) {
			this.bios_date = bd;
			this.bios_version = bve;
			this.bios_vendor = bv;
			this.board_name = board;
		}
		
		public String getDate() {
			return this.bios_date;
		}
		
		public String getBIOSVendor() {
			return this.bios_vendor;
		}
		
		public String getBIOSVersion() {
			return this.bios_version;
		}
		
		public String getBoard() {
			return this.board_name;
		}
		
		@Override
		public String toString() {
			return "BIOS[date:" + this.bios_date + ",version " + this.bios_version + " by " + this.bios_vendor + " on board " + this.board_name + ".]";
		}
	}
	
	public static BIOS parse() {
		if (OSUtil.isLinux()) {
			final File dmi = FileUtil.safeCreate("/sys/class/dmi/id/subsystem/id");
			if (dmi.isDirectory()) {
				try {
					final String bios_data = ProcessUtil.execCmd("cat /sys/class/dmi/id/bios_date");
					final String bios_vendor = ProcessUtil.execCmd("cat /sys/class/dmi/id/bios_vendor");
					final String bios_version = ProcessUtil.execCmd("cat /sys/class/dmi/id/bios_version");
					final String board_name = ProcessUtil.execCmd("cat /sys/class/dmi/id/board_name");
					return new BIOS(bios_data, bios_vendor, bios_version, board_name);
				}catch (Exception e) {
					e.printStackTrace();
				}
			}
		}else if (OSUtil.isWindows()) {
			try {
				final String output = StringsUtil.limitSpaces(ProcessUtil.execCmd("systeminfo"), 1);
				final String[] data = output.split(JVMUtil.lineSep());
				ArrayUtils.printLln(data);
			}catch (Exception e) {}
		}
		return new BIOS("1/1/1970", "N/A", "N/A", "N/A");
	}
}
